package test.my.com.myapplicationtest.write_view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.os.Environment;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.io.File;
import java.lang.ref.SoftReference;
import java.util.logging.Logger;

import test.my.com.myapplicationtest.paint_view.BitmapUtil;

/**
 * Created by ThinkPad on 2016/3/17.
 */
public class NewPaintView extends View {

    private Paint mPaint;
    private Bitmap mOriginalBitmap;
    private Bitmap mCoverBitmap;
    private Path mPath;
    private Canvas mBitCanvas;

    // 此View的宽高
    private int mViewHeight;
    private int mViewWidth;

    private float mX;
    private float mY;

    // bitmap在canvas绘制的位置
    private Rect mPositionRect;
    // bitmapCanvas缩放比例
    private float mWidthScale;
    private float mHeightScale;

    private Context mContext;
    private String originalPath;

    public NewPaintView(Context context) {
        super(context);
        init();
    }

    public NewPaintView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public NewPaintView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);// 抗锯齿
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);// 始末端圆角
        mPaint.setDither(true); // 防抖动
        mPaint.setStrokeWidth(20);
        mPath = new Path();
        originalPath = Environment.getExternalStorageDirectory() + File.separator + "121.jpg";
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(mOriginalBitmap, null, mPositionRect, mPaint);
        canvas.drawBitmap(mCoverBitmap, null, mPositionRect, mPaint);
        mBitCanvas.drawPath(mPath, mPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        mViewWidth = MeasureSpec.getSize(widthMeasureSpec);
        mViewHeight = MeasureSpec.getSize(heightMeasureSpec);
        Log.e("ah", "mViewWidth == " + mViewWidth + "  mViewHeight == " + mViewHeight);

        mOriginalBitmap = BitmapUtil.createOriginalBitmapByPath(originalPath, mViewWidth, mViewHeight);

        calculatePositionRect();

        mCoverBitmap = Bitmap.createBitmap(mPositionRect.width(),mPositionRect.height(),Bitmap.Config.ARGB_8888);
        mBitCanvas = new Canvas(mCoverBitmap);
//        mBitCanvas.translate(mPositionRect.left, -mPositionRect.top);
//        float scale = mWidthScale > mHeightScale ? mWidthScale:mHeightScale;
//        mBitCanvas.scale(scale, scale);

    }


    private void calculatePositionRect() {
        float scale_v = (float)mViewWidth/mViewHeight;
        float scale_b = (float)mOriginalBitmap.getWidth()/mOriginalBitmap.getHeight();
        mPositionRect = new Rect();
        if (scale_b > scale_v) {
            mPositionRect.left = 0;
            mPositionRect.top = mViewHeight / 2 - mOriginalBitmap.getHeight() / 2;
            mPositionRect.right = mViewWidth;
            mPositionRect.bottom = (mViewHeight / 2 - mOriginalBitmap.getHeight() / 2)+(int)(mViewWidth/scale_b);
            mWidthScale = (float)mViewWidth/mOriginalBitmap.getWidth();

        } else if(scale_b < scale_v){
            mPositionRect.left = mViewWidth / 2 - mOriginalBitmap.getWidth() / 2;
            mPositionRect.top = 0;
            mPositionRect.right = (mViewWidth / 2 - mOriginalBitmap.getWidth() / 2)+(int)(mViewHeight*scale_b);
            mPositionRect.bottom = mViewHeight;
            mHeightScale = (float)mViewHeight/mOriginalBitmap.getHeight();
        }
    }

    public void saveDoneImage() {
        String desPath = Environment.getExternalStorageDirectory() + File.separator + System.currentTimeMillis() + ".jpg";
        BitmapUtil.createImage(mOriginalBitmap, desPath);
    }


    public void clearPath(){
        mPath.reset();
        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float currentX = event.getX();
        float currentY = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                float downX = event.getX();
                float downY = event.getY();

                mX = downX;
                mY = downY;

                mPath.moveTo(downX, downY);
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = Math.abs(currentX - mX);
                float dy = Math.abs(currentY - mY);

                //两点之间的距离大于等于3时，生成贝塞尔绘制曲线
                if (dx > 3 || dy > 3) {
                    //设置贝塞尔曲线的操作点为起点和终点的一半
                    float hx = (mX + currentX) / 2;
                    float hy = (mY + currentY) / 2;
                    //二次贝塞尔，实现平滑曲线；mX, mY为操作点，hx, hy为终点
                    mPath.quadTo(mX, mY, hx, hy);
                }
                //第二次执行时，第一次结束调用的坐标值将作为第二次调用的初始坐标值
                mX = currentX;
                mY = currentY;
                break;
            case MotionEvent.ACTION_UP:
                mPath.lineTo(mX, mY);
                break;
        }
        invalidate();
        return true;
    }
}
